package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.DestinationCommend;
import cn.wolfcode.luowowo.article.mapper.DestinationCommendMapper;
import cn.wolfcode.luowowo.article.query.DestinationCommendQuery;
import cn.wolfcode.luowowo.article.service.IDestinationCommendService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationCommendServiceImpl implements IDestinationCommendService {

    @Autowired
    private DestinationCommendMapper commendMapper;

    @Override
    public PageInfo query(DestinationCommendQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(commendMapper.query(qo));
    }



    @Override
    public void saveOrUpdate(DestinationCommend commend) {

        if (commend.getSequence() == null) {
            int max = commendMapper.selectMaxSequence(commend.getId());
            commend.setSequence(max + 1);
        }

        if (commend.getId() != null) {
            commendMapper.updateByPrimaryKey(commend);
        }else {
            commendMapper.insert(commend);
        }
    }

    @Override
    public void delete(Long id) {
        commendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changeStateValue(Long id, int state) {
        commendMapper.updateStateValue(id,state);
    }

    @Override
    public List<List<DestinationCommend>> queryMonthsCommends() {
        List<List<DestinationCommend>> months = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            List<DestinationCommend> list = commendMapper.selectByMonth(i);
            months.add(list);
        }
        return months;
    }

    @Override
    public List<DestinationCommend> list() {
        return commendMapper.selectAll();
    }

    @Override
    public List<DestinationCommend> selectDestTop5() {
        return commendMapper.selectDestTop5();
    }
}
